//
//  WBNetworkTools.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/5.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType {
    case get
    case post
}

class WBNetworkTools: AFHTTPSessionManager {

    static let sharedTools: WBNetworkTools = {
        
        let tools = WBNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
        
    }()
    
    func requestData(type: RequestType,urlStr: String, params: Any?,callBack:@escaping (Any?, Error?)->()) {
        
        if type == .get {
            
            get(urlStr, parameters: params, progress: nil, success: { (_, responseObject) in
                callBack(responseObject, nil)
            }, failure: { (_, error) in
                callBack(nil, error)
            })
            
        } else {
            
            post(urlStr, parameters: params, progress: nil, success: { (_, responseObject) in
                callBack(responseObject, nil)
            }, failure: { (_, error) in
                callBack(nil, error)
            })
            
        }
        
    }
    
}
// MARK: --首页相关接口
extension WBNetworkTools {
    
    /// 获取登录用户及其所关注的微博数据
    ///
    /// - Parameters:
    ///   - token: 令牌
    ///   - callBack: 闭包回调 
    func requestStatuses(token:String,maxId:Int64 = 0 ,sinceId: Int64 = 0,callBack: @escaping (Any?, Error?) -> ()) {
        // 接口地址
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params: [String:Any] = [
            "access_token": token,
            "max_id" : maxId,
            "since_id": sinceId
        ]
        
        // 发送网络请求
        requestData(type: .get, urlStr: urlStr, params: params, callBack: callBack)
    }
    
}
// MARK: --发微博相关接口
extension WBNetworkTools {
    
    /// 发送文字微博
    ///
    /// - Parameters:
    ///   - token: 令牌
    ///   - status: 微博内容
    ///   - callBack: 闭包回调
    func sendMessage(token:String,status:String,callBack: @escaping (Any?, Error?) -> ()) {
        
        let urlStr = "https://api.weibo.com/2/statuses/update.json"
        
        let params = [
        
            "access_token": token,
            "status": status
            
        ]
        
        requestData(type: .post, urlStr: urlStr, params: params, callBack: callBack)
        
    }
    
    /// 发送文字图片微博
    ///
    /// - Parameters:
    ///   - token: 令牌
    ///   - status: 微博内容
    ///   - pic: 微博图片
    ///   - callBack: 闭包的回调
    func sendMessage(token:String,status:String,pic:UIImage,callBack: @escaping (Any?, Error?) -> ()) {
        
        let urlStr = "https://upload.api.weibo.com/2/statuses/upload.json"
        let params = [
            
            "access_token": token,
            "status": status
            
        ]
        
        let imageData = UIImageJPEGRepresentation(pic, 0.5)!
        
        post(urlStr, parameters: params, constructingBodyWith: { (formData) in
            
            formData.appendPart(withFileData: imageData, name: "pic", fileName: "tett", mimeType: "image/jpeg")
        }, progress: nil, success: { (_, response) in
            
            callBack(response,nil)
            
        }) { (_, error) in
            
            callBack(nil,error)
        }
        
    }
    
}

// MARK: --OAuth相关接口
extension WBNetworkTools {
    
    /// 用户数据
    ///
    /// - Parameters:
    ///   - token: 用户令牌
    ///   - uid: 用户ID
    ///   - callBack: 用户信息的闭包
    func requestUserInfo(token:String,uid: Int64,callBack: @escaping (Any?, Error?) -> ()) {
        
        let urlStr = "https://api.weibo.com/2/users/show.json"
        
        let parms: [String:Any] = [
            
            "access_token": token,
            "uid": uid
        ]
        
        requestData(type: .get, urlStr: urlStr, params: parms, callBack: callBack)
    }
    
    /// 请求token
    ///
    /// - Parameters:
    ///   - code: 授权码
    ///   - callBack: 回调闭包
    func requestAccessToken(code:String,callBack:@escaping (Any?, Error?) -> ()) {
        // 请求地址
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
        
            "client_id": AppKey,
            "client_secret": AppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": Redirect_Uri
            
        ]
        requestData(type: .post, urlStr: urlStr, params: params, callBack: callBack)
        
    }

}
